<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-07-01
 */
namespace app\admin\controller;

use think\Captcha;
use think\Controller;
use think\Config;
use think\Session;
use app\common\helper\VerifyHelper;
class LoginController extends BaseController{
	/**
	 * 登录页面
	 */
	public function loginOp(){
		if($_POST){
			$code = input('post.verifycode');
			if(!empty($code)){
				if(VerifyHelper::check($code)){
					$condition['admin_name'] = input('post.username');
					$condition['admin_password'] = input('post.password');
					if(!empty($condition['admin_name']) && !empty($condition['admin_password'])){
						$condition['admin_password'] = encrypt($condition['admin_password']);
						$admin_info = model('Admin')->getAdminInfo($condition);
						if(is_array($admin_info) && !empty($admin_info)){
							session('admin_id',$admin_info['admin_id']);
							session('admin_name',$admin_info['admin_name']);
							session('admin_is_super',$admin_info['admin_is_super']);
							session('admin_role_id',$admin_info['admin_role_id']);
							//获取权限节点
							$this->setRoleNode();
							//更新登录信息
							model('Admin')->update_table('admin','admin_id='.$admin_info['admin_id'],array('login_time'=>time(),'login_num'=>$admin_info['login_num']+1));
							model('AdminLog')->login_log(1,'后台登录成功');
							return json(array('status'=>1));
						}else{
							model('AdminLog')->login_log(0,'用户名或密码不正确');
							return json(array('status'=>1004,'msg'=>'用户名或密码不正确'));
						}
					}else{
						model('AdminLog')->login_log(0,'用户名或密码为空');
						return json(array('status'=>1003,'msg'=>'请填写用户名密码'));
					}
				}else{
					model('AdminLog')->login_log(0,'验证码错误');
					return json(array('status'=>1002,'msg'=>'验证码错误'));
				}
			}else{
				model('AdminLog')->login_log(0,'验证码为空');
				return json(array('status'=>1001,'msg'=>'验证码不能为空'));
			}
		}else{
			return $this->fetch();
		}
	}
	
	/**
	 * 验证码
	 */
	public function captchaOp($id = ""){
        return VerifyHelper::captcha($id);
    }
    /**
     * 退出系统
     */
    public function loginoutOp(){
    	Session::clear();
    	$this->redirect('Login/login');
    }
}